﻿using Microsoft.Xna.Framework;


namespace PoolGame2.Game
{
    public class GameCamera : Camera
    {
        public Vector3 cameraFreeReference;
        public Vector3 cameraFreePosition;

        public GameCamera(PoolGame game, int w, int h) : base( game )
        {
            UpdateProjection(0.1f, 1000.0f, 60.0f);
            cameraPosition = new Vector3(0.0f, 3000.0f, 400.0f);
            cameraFreePosition = new Vector3(0.0f, 400.0f, 0.0f);
            // Uh oh, I think this is _hardcoded_ for the intial position of the cueball
            //cameraPosition = new Vector3(10.0f, -3000.0f, 10.0f);
            cameraReference = new Vector3(-1.0f, 10.3f, 3.0f);
            cameraTarget = cameraReference + cameraPosition;
            updownRot = 0.0f;
            leftrightRot = 0.0f;
            UpdateProjection(0.1f, 1000.0f, 60.0f);
            viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
        }

        /// <summary>Turns the camera so it is facing the point provided</summary>
        /// <param name="lookAtPosition">Position the camera should be pointing to</param>
        public void LookAt(Vector3 lookAtPosition)
        {
            // Use a local variable because we can't take a reference to the value returned
            // by a property get. The ref variant is still faster than copying entire
            // matrices around.
            Vector3 cameraPosition = this.viewMatrix.Translation;
            Matrix.CreateLookAt(ref cameraPosition, ref lookAtPosition, ref up, out this.viewMatrix);
        }


        public override void UpdateCamera(GameTime gametime)
        {
            //Vector3 moveVector = Vector3.Zero;
            //GamePadThumbSticks sticks = GamePad.GetState(PlayerIndex.One).ThumbSticks;

            //if (PoolGame.gameControls.InMenu)
            //    return;

            ////topDown = cont.SwitchToTopDownCamera(topDown);
            //noClip = PoolGame.gameControls.SwitchToNoClip(noClip);

            //if (lookAtTarget == null || noClip)
            //{
            //    // Translational controls
            //    moveVector = PoolGame.gameControls.UpdateCameraPosition(moveVector);

            //    // Rotational controls
            //    cameraYaw = PoolGame.gameControls.UpdateCameraYaw(cameraYaw);
            //    cameraPitch = PoolGame.gameControls.UpdateCameraPitch(cameraPitch);
            //    cameraPosition.Y = PoolGame.gameControls.UpdateCameraY(cameraPosition.Y);

            //    cameraPitch = MathHelper.Clamp(cameraPitch, MathHelper.ToRadians(-149.0f), MathHelper.ToRadians(149.9f));

            //    Matrix cameraViewRotationMatrix = Matrix.CreateRotationX(cameraPitch) * Matrix.CreateRotationY(cameraYaw);
            //    Matrix cameraMoveRotationMatrix = Matrix.CreateRotationY(cameraYaw);

            //    Vector3 transformedCameraReference = Vector3.Transform(cameraReference, cameraViewRotationMatrix);
            //    cameraPosition = Vector3.Add(cameraFreePosition, Vector3.Transform(moveVector, cameraMoveRotationMatrix));
            //    checkCameraCollisions();
            //    cameraFreePosition = cameraPosition;
            //    if (runonce)
            //    {
            //        cameraTarget = new Vector3();
            //            runonce = false;
            //    }
            //    else
            //    {
            //        cameraTarget = Vector3.Add(transformedCameraReference, cameraPosition);
            //    }
            //    viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
            //}
            //else
            //{
            //    if (lookAtLocked == false)
            //    {
            //        // Translational controls
            //        lookAtOffset = PoolGame.gameControls.UpdateCameraLookAtOffset(lookAtOffset);

            //        // Rotational controls
            //        lookAtRotation = PoolGame.gameControls.UpdateCameraLookAtRotation(lookAtRotation);

            //        lookAtRotation.X = MathHelper.Clamp(lookAtRotation.X, MathHelper.ToRadians(-89.9f), MathHelper.ToRadians(89.9f));

            //        Matrix cameraViewRotationMatrix = Matrix.CreateRotationX(lookAtRotation.X) * Matrix.CreateRotationY(lookAtRotation.Y);
            //        Vector3 transformedCameraReference = Vector3.Transform(Vector3.Forward * lookAtOffset, cameraViewRotationMatrix);

            //        cameraPosition = Vector3.Add(lookAtTarget.getPosition(), transformedCameraReference);
            //        checkCameraCollisions();
            //        cameraTarget = lookAtTarget.getPosition();
            //        viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up);
            //    }
            //    else
            //    {
            //        viewMatrix = Matrix.CreateLookAt(cameraPosition, lookAtTarget.getPosition(), Vector3.Up);
            //    }
            //}
        }

        public void checkCameraCollisions()
        {
            if (cameraPosition.Y < 55.0f)
            {
                cameraPosition.Y = 55.0f;
            }
        }



    }
}
